﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapidAddition.FIXEngine;
using LiquidTrading.RapidAdditionHelper.Storage;

namespace LiquidTrading.RapidAdditionHelper.Clients
{
    /// <summary>
    /// Handles everything to do with the connection to the Sterling Drop Copy.
    /// </summary>
    public class SterlingDropCopy : FIXClient
    {
        public SterlingDropCopy(Credentials.FileRepoDBLog creds, IStorage storage)
            : base(creds)
        {
            Storage = storage;
        }

        public SterlingDropCopy(Credentials.FileRepoFileLog creds, IStorage storage)
            : base(creds)
        {
            Storage = storage;
        }

        public SterlingDropCopy(Credentials.TesterFileRepo creds, IStorage storage)
            : base(creds)
        {
            Storage = storage;
        }


        public IStorage Storage { get; private set; }

        public override void Start()
        {
           base.Start();
        }

        public override void NewState(RapidAddition.FIXEngine.CFIXSession Session, RapidAddition.FIXEngine.EFIXSessionState State)
        {
            if (Storage != null)
            {
                Storage.ProcessState(Session, State);
            }
            base.NewState(Session, State);
        }

        public override void ReceiveMessage(RapidAddition.FIXEngine.CFIXSession Session, RapidAddition.FIXEngine.CFIXMessage Message, int SessionID, int ID, bool DoNotProcess)
        {            
            if (Message.MsgType == "8")
            {
                Console.WriteLine("(ExecReport)");
                if (Storage != null)
                    Storage.ProcessExecutionReportFIX42(FIXDataDictionary, Message);
            }
            else if (Message is CFIXMessageLogon)
            {
                Console.WriteLine("(Logon)");
            }
            else if (Message is CFIXMessageHeartbeat)
            {
                //Console.WriteLine("(Heartbeat)");
            }
            else if (Message is CFIXMessageLogout)
            {
                Console.WriteLine("(Logout)");
            }
            else
            {
                Console.WriteLine("(" + Message.MsgType + ")");
            }            
        }


        public override void SendMessage(RapidAddition.FIXEngine.CFIXSession Session, RapidAddition.FIXEngine.CFIXMessage Message)
        {
            base.SendMessage(Session, Message);
        }

    }
}
